*********************************************************************
*This file cleans completions by major field of study from the IPEDS*
*********************************************************************

*** create dta files from csv, labeling
cd "$raw_data_education/completions"

foreach i in 1989 1990 1991 1992 1993 1994 1995 2000 2001{
cap do "$do_clean_education/IPEDS - do/do completions/c`i'_re.do"
keep unitid cipcode awlevel crace03 crace04 crace09 crace10 crace11 crace12 crace15 crace16

gen majors = int(cipcode/10000) 
replace majors = 99 if majors == 0

rename crace03 blackMen
rename crace04 blackWomen
egen black = rsum(blackMen blackWomen)
replace black = . if mi(blackMen) & mi(blackWomen)
label variable black "Black, non-Hispanic total"
rename crace09 hispanicMen
rename crace10 hispanicWomen
egen hispanic = rsum(hispanicMen hispanicWomen)
replace hispanic = . if mi(hispanicMen) & mi(hispanicWomen)
label variable hispanic "Hispanic total"
rename crace11 whiteMen
rename crace12 whiteWomen
egen white = rsum(whiteMen whiteWomen)
replace white = . if mi(whiteMen) & mi(whiteWomen)
label variable white "white total"
rename crace15 men
rename crace16 women
egen total = rsum(men women)
replace total = . if mi(men) & mi(women)
label variable total "Grand total"
gen year = `i'
save "$clean_data_education/Completions_`i'.dta", replace
clear 
}
************************************************************

foreach i in 1999 2006 2007 {
cap do "$do_clean_education/IPEDS - do/do completions/c`i'_a2dig.do"
keep unitid cipcode awlevel crace03 crace04 crace09 crace10 crace11 crace12 ///
crace15 crace16 crace18 crace21 crace22 crace24

gen majors = int(cipcode/10000) 
replace majors = 99 if majors == 0

rename crace03 blackMen
rename crace04 blackWomen
rename crace18 black
rename crace09 hispanicMen
rename crace10 hispanicWomen
rename crace21 hispanic
rename crace11 whiteMen
rename crace12 whiteWomen
rename crace22 white 
rename crace15 men
rename crace16 women
rename crace24 total
gen year = `i'
save "$clean_data_education/Completions_`i'.dta", replace
clear
}
************************************************************
foreach i in 2008 {
cap do "$do_clean_education/IPEDS - do/do completions/c`i'_a2dig.do"
keep unitid cipcode awlevel ctotalm ctotalw ctotalt dvcbkt dvcbkm dvcbkw dvchst dvchsm dvchsw dvcwht dvcwhm dvcwhw

gen majors = int(cipcode/10000) 
replace majors = 99 if majors == 0

rename dvcbkm blackMen
rename dvcbkw blackWomen
rename dvcbkt black
rename dvchsm hispanicMen
rename dvchsw hispanicWomen
rename dvchst hispanic
rename dvcwhm whiteMen
rename dvcwhw whiteWomen
rename dvcwht white 
rename ctotalm men
rename ctotalw women
rename ctotalt total
gen year = `i'
save "$clean_data_education/Completions_`i'.dta", replace
clear
}
************************************************************

foreach i in 2013 2014 2015 {
cap do "$do_clean_education/IPEDS - do/do completions/c`i'_a2dig.do"
keep unitid cipcode awlevel ctotalt ctotalm ctotalw cbkaat cbkaam cbkaaw chispt chispm chispw cwhitt cwhitm cwhitw

gen majors = int(cipcode/10000) 
replace majors = 99 if majors == 0

rename cbkaam blackMen
rename cbkaaw blackWomen
rename cbkaat black
rename chispm hispanicMen
rename chispw hispanicWomen
rename chispt hispanic
rename cwhitm whiteMen
rename cwhitw whiteWomen
rename cwhitt white 
rename ctotalm men
rename ctotalw women
rename ctotalt total
gen year = `i'
save "$clean_data_education/Completions_`i'.dta", replace
clear
}
************************************************************

* append
use "$clean_data_education/Completions_1989.dta", clear
foreach i in 1990 1991 1992 1993 1994 1995 1999 2000 2001 2006 2007 2008 2013 2014 2015 {
append using "$clean_data_education/Completions_`i'.dta"
rm "$clean_data_education/Completions_`i'.dta"
}
rm "$clean_data_education/Completions_1989.dta"

* final cleanings
do "$do_clean_education/clean institutions - id.do"

* NO COLLAPSE HERE: this data must be reshaped 

save "$clean_data_education/completions.dta", replace
